var a = require("../../utils/api.js"), t = getApp();

require("../../utils/disapi.js");

Page({
    data: {
        mscur: "curgoods",
        show: !1,
        choose: {
            num: 1
        },
        limitTips: {
            title: "",
            duration: 2e3,
            show: !1
        },
        showLoding: !1
    },
    onLoad: function(o) {
        var e = this;
        a.getTeamOpenid({
            openid: t.globalData.openid
        }).then(function(a) {
            e.setData({
                head: a.data.head_info,
                member: a.data.user
            });
        }), a.Seckillindex({
            openid: t.globalData.openid
        }).then(function(a) {
            if (console.log(a), a.data.currentgoods.goods && a.data.currentgoods.goods.length > 0) var o = a.data.currentgoods, i = "overgoods" + parseInt(a.data.overgoods.length - 2), d = "curgoods"; else {
                o = a.data.aftergoods.length > 0 ? a.data.aftergoods[0] : a.data.overgoods[0];
                if (0 == a.data.overgoods.length) i = "aftergoods0"; else if (0 == a.data.aftergoods.length) i = "overgoods" + a.data.overgoods.length; else i = "aftergoods0";
                d = a.data.aftergoods.length > 0 ? "aftergoods0" : "overgoods0";
            }
            e.setData({
                user_spike: t.globalData.user_spike,
                seckillGoods: a.data,
                curSeckillGoods: o,
                ind: a.data.aftergoods.length,
                curtime: i,
                mscur: d,
                resource: "spike",
                seckill_goods_list_icon: a.data.seckill_goods_list_icon,
                seckill_goods_info_price_bg: a.data.seckill_goods_info_price_bg,
                showLoding: !0
            }), console.log(e.data.curSeckillGoods);
        });
    },
    mschange: function(a) {
        var t = this;
        console.log(a);
        var o = a.currentTarget.dataset.index || 0 == a.currentTarget.dataset.index ? a.currentTarget.dataset.index : "", e = a.currentTarget.dataset.ms_id;
        if (t.setData({
            showLoding: !1
        }), console.log(e, o), "overgoods" == e) i = t.data.seckillGoods.overgoods[o];
        if ("curgoods" == e) i = t.data.seckillGoods.currentgoods;
        if ("aftergoods" == e) var i = t.data.seckillGoods.aftergoods[o];
        console.log(i), t.setData({
            mscur: e + o,
            curSeckillGoods: i,
            "choose.num": 1,
            showLoding: !0
        });
    },
    addSpec: function(o) {
        var e = this, i = t.globalData.openid, d = o.currentTarget.dataset.id, s = (o.currentTarget.dataset.atid, 
        e.data.info);
        if ("spike" == e.data.resource) {
            if (data.num > e.data.info.maxbuy) return void wx.showToast({
                title: "每单限购" + e.data.info.maxbuy + "份",
                duration: 2e3,
                icon: "none"
            });
            a.Seckill_GetSpecInfo({
                openid: i,
                id: d
            }).then(function(a) {
                console.log(a);
            });
        } else {
            if (0 != parseInt(s.g_limit_num) && parseInt(e.data.curGoodsNum) >= parseInt(s.g_limit_num)) return void wx.showToast({
                title: "本商品限购" + s.g_limit_num + "件",
                icon: "none",
                duration: 2e3
            });
            a.GetSpecInfo({
                openid: i,
                id: d
            }).then(function(t) {
                if (console.log(t), 0 == t.errno) {
                    var o = [], d = [];
                    console.log(t.data.spec);
                    for (var s in t.data.spec) {
                        var n = [], c = [];
                        d[s] = {
                            id: t.data.spec[s].id,
                            subSpec: t.data.spec[s].item[0].gsi_id
                        };
                        for (var g in t.data.spec[s].item) n[g] = t.data.spec[s].item[g].gsi_title, c[g] = t.data.spec[s].item[g].gsi_id;
                        o[s] = {
                            id: t.data.spec[s].id,
                            name: t.data.spec[s].title,
                            stem: n,
                            stem_id: c
                        };
                    }
                    for (var l = "", s = d.length - 1; s >= 0; s--) l += "," + d[s].subSpec;
                    a.GetSpecDeatail({
                        openid: i,
                        id_str: l,
                        gid: t.data.goods.g_id
                    }).then(function(a) {
                        if (console.log(a), 0 == a.errno) {
                            var i = a.data.ggo_market_price.split("."), s = t.data.goods.g_limit_num <= 0 ? a.data.ggo_stock : parseInt(t.data.goods.g_limit_num) < parseInt(a.data.ggo_stock) ? t.data.goods.g_limit_num : a.data.ggo_stock, n = {
                                id: t.data.goods.g_id,
                                name: t.data.goods.g_name,
                                photo: t.data.goods.g_icon,
                                specInfo: a.data.ggo_title,
                                price: i,
                                oldPrice: a.data.ggo_old_price,
                                num: 1,
                                popup: o,
                                count: a.data.ggo_stock,
                                limit: s,
                                is_limit: t.data.goods.g_limit_num,
                                ggo_id: a.data.ggo_id
                            };
                            e.setData({
                                specDetail: n,
                                specShow: !e.data.specShow,
                                specDefault: d
                            });
                        } else wx.showToast({
                            title: t.message,
                            icon: "none"
                        });
                    });
                } else wx.showToast({
                    title: t.message,
                    icon: "none"
                });
            });
        }
    },
    popup: function(o) {
        for (var e = this, i = o.currentTarget.dataset.id, d = o.currentTarget.dataset.subid, s = e.data.specDefault, n = "", c = s.length - 1; c >= 0; c--) s[c].id == i && (s[c].subSpec = d, 
        e.setData({
            specDefault: s
        })), n += "," + s[c].subSpec;
        var g = t.globalData.openid, l = e.data.specDetail;
        "spike" == e.data.resource ? a.Seckill_GetSpecDeatail({
            openid: g,
            id_str: n,
            gid: l.id
        }).then(function(a) {
            if (console.log(a), 0 == a.errno) {
                if (1 == a.data.is_seckill) {
                    var t = a.data.price.split("."), o = a.data.maxbuy <= 0 ? a.data.total : parseInt(a.data.maxbuy) < parseInt(a.data.total) ? a.data.maxbuy : a.data.total;
                    console.log(o), e.setData({
                        is_seckill: 1
                    });
                } else {
                    var t = a.data.ggo_market_price.split("."), o = l.is_limit <= 0 ? a.data.ggo_stock : parseInt(l.limit) < parseInt(a.data.ggo_stock) ? l.limit : a.data.ggo_stock;
                    e.setData({
                        is_seckill: 0
                    });
                }
                console.log(e.data.is_seckill), l.specInfo = a.data.ggo_title, l.price = t, l.oldPrice = a.data.ggo_old_price, 
                l.count = a.data.ggo_stock, l.limit = o, l.ggo_id = a.data.ggo_id, l.num = 1, e.setData({
                    specDetail: l
                });
            } else wx.showToast({
                title: res.message,
                icon: "none"
            });
        }) : a.GetSpecDeatail({
            openid: g,
            id_str: n,
            gid: l.id
        }).then(function(a) {
            if (0 == a.errno) {
                var t = a.data.ggo_market_price.split("."), o = l.is_limit <= 0 ? a.data.ggo_stock : parseInt(l.is_limit) < parseInt(a.data.ggo_stock) ? l.is_limit : a.data.ggo_stock;
                l.specInfo = a.data.ggo_title, l.price = t, l.oldPrice = a.data.ggo_old_price, l.count = a.data.ggo_stock, 
                l.limit = o, l.ggo_id = a.data.ggo_id, e.setData({
                    specDetail: l
                });
            } else wx.showToast({
                title: res.message,
                icon: "none"
            });
        });
    },
    onChange: function(a) {
        this.setData({
            "choose.num": a.detail
        });
    },
    specOnChange: function(a) {
        var t = a.detail, o = this, e = o.data.specDetail;
        e.num = t, o.setData({
            specDetail: e
        });
    },
    specAddCart: function(o) {
        var e = this, i = o.currentTarget.dataset.id, d = o.currentTarget.dataset.optionid, s = o.currentTarget.dataset.num, n = o.currentTarget.dataset.atid;
        e.data.info;
        a.SpecAddCart({
            openid: t.globalData.openid,
            gid: i,
            ggoid: d,
            num: s,
            at_id: n
        }).then(function(a) {
            0 == a.errno ? (e.successtip(), wx.setStorage({
                key: "cartnum",
                data: parseInt(a.data.count)
            }), 0 != a.data.count ? (wx.showTabBarRedDot({
                index: 2
            }), wx.setTabBarBadge({
                index: 2,
                text: a.data.count + ""
            })) : wx.hideTabBarRedDot({
                index: 2
            }), e.setData({
                specShow: !1,
                curGoodsNum: parseInt(e.data.curGoodsNum) + 1
            })) : wx.showToast({
                title: a.message,
                icon: "none",
                duration: 2e3
            });
        });
    },
    buy: function(o) {
        var e = this;
        console.log(o);
        for (var i = 0; i < e.data.curSeckillGoods.goods.length; i++) if (o.currentTarget.dataset.id == e.data.curSeckillGoods.goods[i].goodsid) {
            var d = e.data.curSeckillGoods.goods[i];
            e.setData({
                curbindgoods: d
            });
        }
        if (console.log(e.data.curbindgoods, e.data.choose.num), e.data.show) {
            if ("spike" == e.data.resource) {
                s = {
                    gid: e.data.curbindgoods.goodsid,
                    num: e.data.choose.num,
                    openid: t.globalData.openid,
                    timeid: e.data.curbindgoods.timeid,
                    roomid: e.data.curbindgoods.roomid,
                    taskid: e.data.curbindgoods.taskid
                };
                console.log(e.data.choose.num), console.log(e.data.curbindgoods.goodsid), console.log(s);
            } else var s = {
                gid: e.data.curbindgoods.g_id,
                num: e.data.choose.num,
                openid: t.globalData.openid,
                at_id: e.data.at_id,
                price: e.data.curbindgoods.g_price
            };
            a.CheckGoods({
                id: e.data.curbindgoods.g_id
            }).then(function(a) {
                if (console.log(a), 1 == a.errno && wx.showToast({
                    title: a.message,
                    icon: "none"
                }), 0 == a.errno) {
                    var t = JSON.stringify(s);
                    wx.navigateTo({
                        url: "/pages/checkout/detailCheckout?data=" + t + "&&resource=" + e.data.resource
                    });
                }
            });
        }
        var n = parseInt(d.maxbuy), c = parseInt(d.total);
        if (console.log(n, c), 0 != n && n >= c) g = !0;
        if (0 != n && n < c) g = !1;
        if (0 == n) var g = !0;
        console.log(g), e.setData({
            show: !0,
            spShow: g
        }), console.log(e.data.spShow);
    },
    buyIntime: function(o) {
        for (var e = this, i = 0; i < e.data.curSeckillGoods.goods.length; i++) if (o.currentTarget.dataset.id == e.data.curSeckillGoods.goods[i].goodsid) {
            var d = e.data.curSeckillGoods.goods[i];
            e.setData({
                curbindgoods: d,
                "choose.num": 1
            });
        }
        if ("spike" == e.data.resource) if (console.log(e.data.curbindgoods, e.data.is_seckill), 
        1 == e.data.is_seckill) {
            n = {
                gid: e.data.curbindgoods.g_id,
                num: e.data.specDetail.num,
                openid: t.globalData.openid,
                ggo_id: e.data.specDetail.ggo_id,
                ggo_title: e.data.specDetail.specInfo,
                timeid: e.data.curbindgoods.timeid,
                roomid: e.data.curbindgoods.roomid,
                taskid: e.data.curbindgoods.taskid
            };
            console.log(n);
            var s = JSON.stringify(n);
            wx.navigateTo({
                url: "/pages/checkout/detailCheckout?data=" + s + "&&resource=spike"
            });
        } else {
            n = {
                gid: e.data.curbindgoods.g_id,
                num: e.data.specDetail.num,
                openid: t.globalData.openid,
                at_id: e.data.at_id,
                price: e.data.specDetail.price[0] + "." + e.data.specDetail.price[1],
                ggo_id: e.data.specDetail.ggo_id,
                ggo_title: e.data.specDetail.specInfo
            };
            console.log(n), a.CheckGoods({
                id: e.data.curbindgoods.g_id
            }).then(function(a) {
                if (1 == a.errno && wx.showToast({
                    title: a.message,
                    icon: "none"
                }), 0 == a.errno) {
                    var t = JSON.stringify(n);
                    wx.navigateTo({
                        url: "/pages/checkout/detailCheckout?data=" + t
                    });
                }
            });
        } else {
            var n = {
                gid: e.data.curbindgoods.g_id,
                num: e.data.specDetail.num,
                openid: t.globalData.openid,
                at_id: e.data.at_id,
                price: e.data.specDetail.price[0] + "." + e.data.specDetail.price[1],
                ggo_id: e.data.specDetail.ggo_id,
                ggo_title: e.data.specDetail.specInfo
            };
            console.log(e.data, n), a.CheckGoods({
                id: e.data.curbindgoods.g_id
            }).then(function(a) {
                if (1 == a.errno && wx.showToast({
                    title: a.message,
                    icon: "none"
                }), 0 == a.errno) {
                    var t = JSON.stringify(n);
                    wx.navigateTo({
                        url: "/pages/checkout/detailCheckout?data=" + t
                    });
                }
            });
        }
    },
    buySpec: function(o) {
        var e = this, i = t.globalData.openid, d = o.currentTarget.dataset.id;
        console.log(d), "spike" == e.data.resource ? a.Seckill_GetSpecInfo({
            openid: i,
            id: d
        }).then(function(t) {
            if (console.log(t), 0 == t.errno) {
                var o = [], d = [];
                console.log(t.data.spec);
                for (var s in t.data.spec) {
                    var n = [], c = [];
                    d[s] = {
                        id: t.data.spec[s].id,
                        subSpec: t.data.spec[s].item[0].gsi_id
                    };
                    for (var g in t.data.spec[s].item) n[g] = t.data.spec[s].item[g].gsi_title, c[g] = t.data.spec[s].item[g].gsi_id;
                    o[s] = {
                        id: t.data.spec[s].id,
                        name: t.data.spec[s].title,
                        stem: n,
                        stem_id: c
                    };
                }
                console.log(1);
                for (var l = "", s = d.length - 1; s >= 0; s--) l += "," + d[s].subSpec;
                a.Seckill_GetSpecDeatail({
                    openid: i,
                    id_str: l,
                    gid: t.data.goods.g_id
                }).then(function(a) {
                    if (console.log(a), 0 == a.errno) {
                        if (1 == a.data.is_seckill) {
                            var i = a.data.price.split("."), s = a.data.maxbuy <= 0 ? a.data.total : parseInt(a.data.maxbuy) < parseInt(a.data.total) ? a.data.maxbuy : a.data.total;
                            e.setData({
                                is_seckill: 1
                            });
                        } else {
                            var i = a.data.ggo_market_price.split("."), s = t.data.goods.g_limit_num <= 0 ? a.data.ggo_stock : parseInt(t.data.goods.g_limit_num) < parseInt(a.data.ggo_stock) ? t.data.goods.g_limit_num : a.data.ggo_stock;
                            e.setData({
                                is_seckill: 0
                            });
                        }
                        console.log(e.data.is_seckill);
                        var n = {
                            id: t.data.goods.g_id,
                            name: t.data.goods.g_name,
                            photo: t.data.goods.g_icon,
                            specInfo: a.data.ggo_title,
                            price: i,
                            oldPrice: a.data.ggo_old_price,
                            num: 1,
                            popup: o,
                            count: a.data.total,
                            limit: s,
                            is_limit: t.data.goods.g_limit_num,
                            ggo_id: a.data.ggo_id
                        };
                        e.setData({
                            specDetail: n,
                            specShow: !e.data.specShow,
                            specDefault: d,
                            buyIntime: "buyIntime"
                        });
                    } else wx.showToast({
                        title: t.message,
                        icon: "none"
                    });
                });
            } else wx.showToast({
                title: t.message,
                icon: "none"
            });
        }) : a.GetSpecInfo({
            openid: i,
            id: d
        }).then(function(t) {
            if (console.log(t), 0 == t.errno) {
                var o = [], d = [];
                console.log(t.data.spec);
                for (var s in t.data.spec) {
                    var n = [], c = [];
                    d[s] = {
                        id: t.data.spec[s].id,
                        subSpec: t.data.spec[s].item[0].gsi_id
                    };
                    for (var g in t.data.spec[s].item) n[g] = t.data.spec[s].item[g].gsi_title, c[g] = t.data.spec[s].item[g].gsi_id;
                    o[s] = {
                        id: t.data.spec[s].id,
                        name: t.data.spec[s].title,
                        stem: n,
                        stem_id: c
                    };
                }
                for (var l = "", s = d.length - 1; s >= 0; s--) l += "," + d[s].subSpec;
                a.GetSpecDeatail({
                    openid: i,
                    id_str: l,
                    gid: t.data.goods.g_id
                }).then(function(a) {
                    if (console.log(a), 0 == a.errno) {
                        var i = a.data.ggo_market_price.split("."), s = t.data.goods.g_limit_num <= 0 ? a.data.ggo_stock : parseInt(t.data.goods.g_limit_num) < parseInt(a.data.ggo_stock) ? t.data.goods.g_limit_num : a.data.ggo_stock, n = {
                            id: t.data.goods.g_id,
                            name: t.data.goods.g_name,
                            photo: t.data.goods.g_icon,
                            specInfo: a.data.ggo_title,
                            price: i,
                            oldPrice: a.data.ggo_old_price,
                            num: 1,
                            popup: o,
                            count: a.data.ggo_stock,
                            limit: s,
                            is_limit: t.data.goods.g_limit_num,
                            ggo_id: a.data.ggo_id
                        };
                        e.setData({
                            specDetail: n,
                            specShow: !e.data.specShow,
                            specDefault: d,
                            buyIntime: "buyIntime"
                        });
                    } else wx.showToast({
                        title: t.message,
                        icon: "none"
                    });
                });
            } else wx.showToast({
                title: t.message,
                icon: "none"
            });
        });
    },
    onCloseButton: function(a) {
        this.setData({
            specShow: !1,
            "choose.num": 1
        });
    },
    onCloseButtons: function(a) {
        this.setData({
            show: !1,
            "choose.num": 1
        });
    },
    maxLimit: function(a) {
        var t = this;
        console.log(a);
        var o = a.currentTarget.dataset.max;
        t.data.spShow ? t.showtips({
            title: "商品数量没有那么多了",
            duration: 2e3
        }) : t.showtips({
            title: "商品限购" + o + "件哦",
            duration: 2e3
        });
    },
    overlimit: function(a) {
        var t = this;
        console.log(a);
        var o = a.currentTarget.dataset.max;
        t.data.spShow ? t.showtips({
            title: "商品限购" + o + "件哦",
            duration: 2e3
        }) : t.showtips({
            title: "商品数量没有那么多了",
            duration: 2e3
        });
    },
    showtips: function(a) {
        var t = this, o = a.title, e = a.duration ? a.duration : null;
        console.log(o, e);
        i = {
            title: o,
            duration: null == e ? null : e,
            show: !0
        };
        if (t.setData({
            limitTips: i
        }), null != e) {
            var i = {
                title: "",
                duration: 2e3,
                show: !1
            };
            setTimeout(function() {
                t.setData({
                    limitTips: i
                });
            }, e);
        }
    },
    hidetips: function() {
        var a = {
            title: "",
            duration: 2e3,
            show: !1
        };
        this.setData({
            limitTips: a
        });
    }
});